<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-02-24
 * Time: 13:21
 */

namespace app\api\controller;

use app\common\Base;
use app\model\Customer;
use app\tools\MIniprogram;
use app\tools\RedisCache;
use app\tools\Wx;
use think\Request;

class User extends Base{

    public $wechat = [];

    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->wechat['appid'] = config('mini.appid');
        $this->wechat['secret'] = config('mini.secret');
    }

    function customer(){
        $code = $this->getParam('code', '');
        $promo_code = $this->getParam('promo_code', '');
        $encryptedData = $this->getParam('encryptedData', '');
        $iv = $this->getParam('iv', '');
//        $iv = rawurldecode($iv);
//        $iv = str_replace(" ", "+", $iv);

        $data = MIniprogram::code2session($code, $this->wechat);
        $session_key = $data['session_key'];
        unset($data['session_key']);

//        file_put_contents('1.txt', $session_key);

        if($encryptedData){
            $data = MIniprogram::decryptData($this->wechat['appid'], $session_key, $iv, $encryptedData );
        }

        $result = Customer::updateOrCreate($data);
        RedisCache::getInstance()->set('session_key_'.$result['id'], $session_key);
        RedisCache::getInstance()->hMSet("user_info_".$result['id'], $result);

        if($promo_code){
            $key = "come_uid".$result['u_id'];
            if(!RedisCache::getInstance()->get($key)){
                RedisCache::getInstance()->set($key, 1);
                $parent = db('customer')->where(['code'=> $promo_code])->field('id, unionid')->find();
                $res = Customer::add_money([
                    'u_id'=> $result['id'],
                    'parent_id'=> $parent['id'],
                    'is_new'=> $result['is_new']
                ]);

                if($res){
                    $wx = new Wx();

                    $openid = db('customer')->where(['unionid'=> $parent['unionid'], 'type'=> 2])->value('openid');

                    $wx->sendTemplate([
                        'openid'=> $openid,
                        'page'=> 'pages/mine/mine',
                        'data'=> [
                            "first"=> [
                                "value"=> "收益到账通知",
                            ],
                            "keyword1"=> [
                                "value"=> "获得 30 虎牙",
                            ],
                            "keyword2"=> [
                                "value"=> date('Y-m-d H:i:s'),
                            ],
                        ]
                    ]);
                }

                RedisCache::getInstance()->delete($key);
            }else{
                return fail('OPERATION_OFTEN');
            }
        }

        return success($result);
    }

    function updateSessionKey()
    {
        $code = $this->getParam('code', 0);
        $data = MIniprogram::code2session($code, $this->wechat);
        return success([]);
    }

}